﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.IWorkerEvaluationB;
using StartsEAS_API.DAL.WorkerEvaluationD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.WorkerEvaluationB
{
    public class WorkerEvaluationBLL : IWorkerEvaluationBLL
    {
        private readonly StarsEASContext _context;
        public WorkerEvaluationDAL _workerEvaluationDal;

        public WorkerEvaluationBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _workerEvaluationDal = new(_context);
        }

        public ResultUtils AddWorkerEvaluation(string token, JObject workerEvaluationJSON)
        {
            if (workerEvaluationJSON == null) return new ResultUtils() { code = 500, message = "workerEvaluation为空" };
            string jsonStrs = JsonConvert.SerializeObject(workerEvaluationJSON);
            WorkerEvaluation workerEvaluation = JsonConvert.DeserializeObject<WorkerEvaluation>(jsonStrs);
            int result = _workerEvaluationDal.AddWorkerEvaluation(token, workerEvaluation);
            if (result == 1) {
                return new ResultUtils() { code = 200, message ="添加成功"};
            }
            return new ResultUtils() { code = 500, message = "操作异常" };
        }

        public ResultUtils GetWorkerEvaluation(string token)
        {
            return _workerEvaluationDal.GetWorkerEvaluation(token);
        }

        public ResultUtils GetWorkerEvaluationsByCourseId(string token, int CourseId)
        {
            IEnumerable<Object> workerEvaluations = _workerEvaluationDal.GetWorkerEvaluationsByCourseId(token, CourseId);
            if (workerEvaluations == null) return new ResultUtils() { code = 500, message = "数据为空" };
            return new ResultUtils() { code = 200, message = workerEvaluations };
        }

        public ResultUtils GetWorkerEvaluationsByStuId(string token, string StuId)
        {
            IEnumerable<Object> workerEvaluations = _workerEvaluationDal.GetWorkerEvaluationsByStuId(token, StuId);
            if (workerEvaluations == null) return new ResultUtils() { code = 500, message = "数据为空" };
            return new ResultUtils() { code = 200, message = workerEvaluations };
        }

        public ResultUtils GetWorkerEvaluationsByWorkerId(string token, string workerId)
        {
            IEnumerable<Object> workerEvaluations = _workerEvaluationDal.GetWorkerEvaluationsByWorkerId(token, workerId);
            if (workerEvaluations == null) return new ResultUtils() { code = 500, message = "数据为空" };
            return new ResultUtils() { code = 200, message = workerEvaluations };
        }
    }
}
